﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SEVN.Web.Framework.Controls
{
    public partial class BodyWebControl : System.Web.UI.UserControl
    {
        WebFrameDataContext db = new WebFrameDataContext();
        public static bool HasRight = true;
        public static bool HasLeft = false;
        public static MyPage CurrentPage = new MyPage();
        public static Skin CurrentSkin = new Skin();

        private string _pageName;

        public string PageName
        {
            get { return _pageName; }
            set { _pageName = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CurrentPage = db.MyPages.SingleOrDefault(m => m.PageName == _pageName);
                if (CurrentPage != null)
                {
                    SetPosition(CurrentPage.Skin);
                    CurrentSkin = CurrentPage.Skin;
                    LoadAllControlToSkin();
                    Ultility.LoadAllControlToPage(this.Page, placeContent, CurrentPage, 'C');
                }
            }
        }

        private void LoadAllControlToSkin()
        {
            //placeHead.Controls.Clear();
            //placeContent.Controls.Clear();
            //placeLeft.Controls.Clear();
            //placeRight.Controls.Clear();
            //placeFooter.Controls.Clear();

            Ultility.LoadAllControlToSkin(this.Page, placeHead, CurrentSkin, 'T');
            if (HasLeft)
                Ultility.LoadAllControlToSkin(this.Page, placeLeft, CurrentSkin, 'L');
            if (HasRight)
                Ultility.LoadAllControlToSkin(this.Page, placeRight, CurrentSkin, 'R');
            Ultility.LoadAllControlToSkin(this.Page, placeContent, CurrentSkin, 'C');
            Ultility.LoadAllControlToSkin(this.Page, placeFooter, CurrentSkin, 'B');
        }

        private void SetPosition(Skin sk)
        {
            // Load Preview
            if (sk.Position.Contains('L'))
            {
                HasLeft = true;
            }
            else
                HasLeft = false;

            if (sk.Position.Contains('R'))
                HasRight = true;
            else
                HasRight = false;

            if (HasLeft && HasRight)
            {
                pnlLeft.CssClass = "left2";
                pnlContent.CssClass = "content2";
                pnlRight.CssClass = "right2";
            }
            else
            {
                pnlLeft.CssClass = "left";
                pnlContent.CssClass = "content";
                pnlRight.CssClass = "right";
            }
        }
    }
}